package com.ruoyi.patrol.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.patrol.domain.PtFormDetails;
import com.ruoyi.patrol.domain.PtFormDetailsOption;
import com.ruoyi.patrol.domain.vo.PtFormDetailVo;
import com.ruoyi.patrol.mapper.PtFormFillingMapper;
import com.ruoyi.patrol.service.IPtFormDetailsOptionService;
import com.ruoyi.patrol.service.IPtFormDetailsService;
import com.ruoyi.patrol.service.IPtFormFillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtFormMapper;
import com.ruoyi.patrol.domain.PtForm;
import com.ruoyi.patrol.service.IPtFormService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 单Service业务层处理
 *
 * @author lkm
 * @date 2022-11-05
 */
@Service
public class PtFormServiceImpl implements IPtFormService {
    @Autowired
    private PtFormMapper ptFormMapper;

    @Autowired
    private IPtFormFillingService iPtFormFillingService;

    @Autowired
    private IPtFormDetailsService iPtFormDetailsService;

    @Autowired
    private IPtFormDetailsOptionService iPtFormDetailsOptionService;

    /**
     * 查询单
     *
     * @param id 单ID
     * @return 单
     */
    @Override
    public PtForm selectPtFormById(Long id) {
        return ptFormMapper.selectPtFormById(id);
    }

    /**
     * 查询单列表
     *
     * @param ptForm 单
     * @return 单
     */
    @Override
    public List<PtForm> selectPtFormList(PtForm ptForm) {
        return ptFormMapper.selectPtFormList(ptForm);
    }

    /**
     * 新增单
     *
     * @param ptFormDetailVo 单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtForm(PtFormDetailVo ptFormDetailVo) {
        PtForm ptForm = new PtForm();
        ptForm.setCreateTime(DateUtils.getNowDate());
        ptForm.setFormName(ptFormDetailVo.getFormTitle());
        ptForm.setCreateBy(SecurityUtils.getUsername());
        int i = ptFormMapper.insertPtForm(ptForm);
        iPtFormFillingService.insertPtFormFilling(ptFormDetailVo.getSchools(), ptForm.getId());
        List<PtFormDetailsOption> ptFormDetailsOptions = new ArrayList<>();
        System.out.println("-------------------1");
        List<PtFormDetails> ptFormDetails = iPtFormDetailsService.insertPtFormDetails(ptFormDetailVo.getFormlist(), ptForm.getId());
        for (int i1 = 0; i1 < ptFormDetails.size(); i1++) {
            PtFormDetails details = ptFormDetails.get(i1);
            if (details.getType() == 0) {
                ptFormDetailVo.getFormlist().get(i1).getOptionlist().forEach(v -> {
                    PtFormDetailsOption ptFormDetailsOption = new PtFormDetailsOption();
                    ptFormDetailsOption.setFromDetailsId(details.getId());
                    ptFormDetailsOption.setFormDetailsOptionName(v.getOption());
                    ptFormDetailsOption.setReason((long) (v.isReason() ? 1 : 0));
                    ptFormDetailsOptions.add(ptFormDetailsOption);
                });
            }
        }
        System.out.println("-------" + ptFormDetails);
        System.out.println("选项总数量-----------" + ptFormDetailsOptions.size());
        if (ptFormDetailsOptions.size() > 0) {
            iPtFormDetailsOptionService.insertPtFormDetailsOption(ptFormDetailsOptions);
        }
        return i;
    }

    /**
     * 修改单
     *
     * @param ptForm 单
     * @return 结果
     */
    @Override
    public int updatePtForm(PtForm ptForm) {
        ptForm.setUpdateTime(DateUtils.getNowDate());
        return ptFormMapper.updatePtForm(ptForm);
    }

    /**
     * 批量删除单
     *
     * @param ids 需要删除的单ID
     * @return 结果
     */
    @Override
    public int deletePtFormByIds(Long[] ids) {
        return ptFormMapper.deletePtFormByIds(ids);
    }

    /**
     * 删除单信息
     *
     * @param id 单ID
     * @return 结果
     */
    @Override
    public int deletePtFormById(Long id) {
        return ptFormMapper.deletePtFormById(id);
    }
}
